package com.ly.mp.busicen.common.helper;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import com.ly.mp.busicen.cache.springcache.jedis.MpCache;
import com.ly.mp.component.entities.UserEntity;

public class SessionHelper {
    private static final Logger logger = LoggerFactory.getLogger(SessionHelper.class);
    public static final String sessionCacheName = "sessionCache";
    static CacheManager cacheManager = (CacheManager) SpringContextHolder.getBean("sessionManager");
    static Cache cache;

    public static void put(String key, Object value) {
        cache.put(key, value);
    }

    public static Object get(String key) {
        Cache.ValueWrapper value = null;

        try {
            value = ((MpCache) cache).getSession(key);
            return value != null ? value.get() : null;
        } catch (Exception var6) {
            try {
                ILog log = (ILog) SpringContextHolder.getBean("logEventBiz", ILog.class);
                UserEntity userEntity = log.getUserEntity(key);
                return userEntity != null && StringUtils.isNotBlank(userEntity.getUserID())? userEntity : null;
            } catch (Exception var5) {
                logger.error("连不上redis, 从数据库中取登录信息出错, " + var5.getMessage());
                return null;
            }
        }
    }

    public static Object getAndNotUpdateExpires(String key) {
        Cache.ValueWrapper value = ((MpCache) cache).get(key, false);
        return value != null ? value.get() : null;
    }

    public static void remove(String key) {
        cache.evict(key);
    }

    public static boolean removeAll() {
        cache.clear();
        return true;
    }

    public static int getPlatformType() {
        return 1;
    }

    public static Cache getCache() {
        return cache;
    }

    static {
        cache = cacheManager.getCache("sessionCache");
    }
}
